/**    
 * @Description: 博客园地址：https://www.cnblogs.com/zumengjie/      
 * @author: 顶风少年   
 * @date: 2021年4月5日 下午10:30:14
 */
package datang.dtdb.scripting;

import datang.dtdb.builder.SqlSourceBuilder;
import datang.dtdb.mapping.BoundSql;
import datang.dtdb.mapping.SqlSource;
import datang.dtdb.session.Configuration;

/**
 * 包含动态标签或者${},这样的sql脚本每次执行都不一样
 */
public class DynamicSqlSource implements SqlSource {
	private MixedSqlNode rootSqlNode;
	private Configuration configuration;
	public DynamicSqlSource(Configuration configuration,MixedSqlNode rootSqlNode) {
		this.rootSqlNode = rootSqlNode;
		this.configuration = configuration;
	}

	/**
	 * 获取boundSql
	 * */
	@Override
	public BoundSql getBoundSql(Object parameterObject) {
		//通过上下文对象获取静态SQL
		DynamicContext context = new DynamicContext(configuration,parameterObject);
		rootSqlNode.apply(context);
		String sql = context.getSql();
		//获取StaticSqlSource
		SqlSourceBuilder sqlSourceBuilder = new SqlSourceBuilder();
		SqlSource sqlSource = sqlSourceBuilder.parse(sql);
		BoundSql boundSql = sqlSource.getBoundSql(null);
		return boundSql;
	}

}
